/**
 * @author hefuxiang@agree.com.cn
 * Date: 2017.04.03
 */
(/* <global> */function (undefined) {

    (function (factory) {
        "use strict";

        // amd module
        if (typeof define === "function" && define.amd) {
            define(["jquery", "widget", 'component.foundationForm'], factory);
        }
        // global
        else {
            factory();
        }

    })
    (function ($, widget) {
        "use strict";

        function render($widget, $selector, option, css, attr, formOption, auiCtx, oWidget) {
            formOption = formOption || {};

            var horizontalTemp = '<label title="_label_" style="margin: 0">_label_</label><div class="text-div"></div>',
                inlineTemp = "<div class='columns'>" +
                    "<label for='middle-label' class='text-right middle' title='_label_' style='margin: 0'>_label_</label>" +
                    "</div>" +
                    "<div class='columns'>" +
                    "<div class='text-div'></div>" +
                    "</div>",
                isInline = formOption.formLayout === 'inline',
                labelAlign = option.labelAlign || formOption.labelAlign,
                template = isInline ? inlineTemp : horizontalTemp,
                span = option.span || formOption.span,
                labelSpan = option.labelSpan || formOption.labelSpan,
                details = option.details || formOption.details,
                $div, style, selectId;

            $selector.attr("id", attr.id || '');
            if (auiCtx) {
                template = template
                    .replace(/_label_/g, $AW.nsl(option.label, $selector.attr('id'), auiCtx) || "");
            } else {
                template = template
                    .replace(/_label_/g, oWidget.nsl(option.label) || "");
            }


            if ($widget && $widget.length) {
                $selector.append(template);
                widget.component.foundationForm.utils_.renderContainerSpan($widget, span);

                if (isInline) {
                    var labelScale = widget.component.foundationForm.utils_.computeLabelscale(span, labelSpan, attr);
                    widget.component.foundationForm.utils_.renderEditorInlineLabelSpan(
                        $selector,
                        labelScale,
                        12 - labelScale,
                        labelAlign);
                }
            } else {
                if (isInline) {
                    $selector = widget.component.foundationForm.utils_.renderRunnerInlineLabelSpan($selector,
                        template,
                        labelSpan,
                        labelAlign,
                        auiCtx,
                        "aweb4FoundationText",
                        attr);
                    $selector.attr('id', attr.id);
                } else {
                    $selector.append(template);
                    widget.component.foundationForm.utils_.renderContainerSpan($selector, span);
                }
            }
            $div = $selector.find('div.text-div');
            option.content && $div.html(option.content);

            //自定义样式
            if (css && css.cssCode && css.cssCode.className) {
                $selector.addClass(css.cssCode.className)
            }
            //样式解析配置
            if (css && (style = css.style)) {
                if (style.title) {
                    if (!isInline) {
                        $div.prev('label').css(style.title);
                    } else {
                        if (typeof auiCtx === 'undefined') {
                            $selector.children().first().find('label').css(style.title);
                        } else {
                            $selector.prev().find('label').css(style.title);
                        }
                    }
                }
                style.position && $selector.css(style.position);
                style.content && $div.css(style.content);
            }

            return {
                setValue: function (data) {
                    var arr,
                        templateHtml = '',
                        flag = true;
                    try {
                        arr = JSON.parse(data)
                    } catch (e) {
                        flag = false;
                        console.log('JSON数据不符合规范');
                    }
                    if (Array.isArray(arr) && flag) {
                        arr.forEach(function (item, index) {
                            var detailsHtml = details ? '<div class="inlineTextBox-details canTrigger">详情</div>' : '',
                                isNew = item.type === 'new' ? "inlineTextBox-red-point" : '';
                            templateHtml += '<div class="inlineTextBox-informItem">' +
                                '<div class="' + isNew + ' inlineTextBox-normal inlineTextBox-content">' + item.content + '</div>' +
                                '<div class="inlineTextBox-isRead canTrigger" data-id="'+item.id+'">已读</div>' +
                                detailsHtml +
                                '</div>';
                        })
                        $div.html(templateHtml);
                        $('.canTrigger').hide();
                        var triggerClass = details ? "inlineTextBox-readAndDetail" : "inlineTextBox-onlyRead";
                        $('.inlineTextBox-informItem').mouseenter(function (e) {
                            var $target = $(e.target);
                            $target.closest('.inlineTextBox-informItem').find('.inlineTextBox-content').removeClass("inlineTextBox-normal").addClass(triggerClass);
                            $target.closest('.inlineTextBox-informItem').find('.canTrigger').show();
                            $target.closest('.inlineTextBox-informItem').find('.canTrigger').addClass('active');

                        }).mouseleave(function (e) {
                            var $target = $(e.target);
                            $target.closest('.inlineTextBox-informItem').find('.inlineTextBox-content').removeClass(triggerClass).addClass("inlineTextBox-normal");
                            $target.closest('.inlineTextBox-informItem').find('.canTrigger').hide();
                            $target.closest('.inlineTextBox-informItem').find('.canTrigger').removeClass('active');

                        });
                    } else {
                        $div.html('数据类型有误，请检查后台数据');
                    }
                },
                getValue: function () {
                    return $div.html();
                },
                // 一个行为类型方法的 实现
                display: function (result, input1, input2, condition) {
                    this[result ? 'hide' : 'show']();
                },

                /*
                 *   @show   弹窗行为
                 *       @e      Event Handler   事件句柄
                 *       @size   Object
                 *           @height     String  高度
                 *           @width      String  宽度
                 *   占位符为 ##_var##.接口名(e,size);
                 * */
                show: function () {
                    $selector.removeClass('hide');
                    isInline && $selector.prev(".columns").removeClass('hide');
                },

                /*
                 *   @hide   隐藏行为
                 * */
                hide: function () {
                    $selector.addClass('hide');
                    isInline && $selector.prev(".columns").addClass('hide');
                },
                getSelect: function () {
                    selectId=this._getSelect();
                    return selectId;
                },
                _getSelect:function () {
                    var id;
                    Array.prototype.slice.call($('.inlineTextBox-isRead')).forEach(function (item) {
                        $(item).hasClass('active')?id=$(item).data('id'):'';
                    });
                    return id
                }
            }
        }

        if (!widget.component.foundationForm) {
            widget.component.foundationForm = {};
        }

        widget.custom.inlineTextBox = function (obj, oOption, oAttr, oCss, auiCtx) {
            var oWidget, $widget, attr, option, css,
                formWidget,
                $selector, formOption;

            if (obj.version) {
                oWidget = obj;
                $widget = oWidget[0].$widget;

                /* 容器类组件需删除这段代码--start */
                $widget.children(':gt(1)').remove();
                $widget.append(oWidget[0].widget.template);
                /* 容器类组件需删除这段代码--end */

                $selector = $widget.children(':last');
                option = oWidget.option();
                formWidget = oWidget.closest('foundationFormCtn');
                if (formWidget.length) {
                    formOption = formWidget.option();
                } else {
                    formOption = {};
                }
                attr = oWidget.attr();
                css = oWidget.css();

                /*编辑阶段的渲染代码*/
                render($widget, $selector, option, css, attr, formOption, auiCtx, oWidget);
            } else {
                $selector = obj;
                option = oOption;
                formOption = auiCtx.configs[$selector.closest('[data-widget-type=aweb4FoundationFormCtn]').attr('id')];
                attr = oAttr;
                css = oCss;

                /*编译阶段渲染代码*/
                return render($widget, $selector, option, css, attr, formOption, auiCtx);
            }
        };

        return widget;
    });
})();
